$(document).on("click", "#game_guide .game_tip_btn", function (e) {
	$("#game_guide").hide();
	return false;
});
// 点击道具按钮
function doFlicker(btnItem, name){
	//判断道具按钮是否可点击
	if($('.game_time').hasClass("playing") || $(btnItem).hasClass("enabled")){
		return false; 
	}
	startTime(name);
	//点击后按钮变灰
	$(btnItem).find("div").eq(0).addClass("on");
	$(btnItem).addClass("enabled");
    $(btnItem).removeClass("opacity").siblings("div").addClass("opacity");
	var selectorName = "div.bug img";
	switch(name){
	  case "fire":
		selectorName = "div.bug img.c_1, div.bug img.c_2, div.bug img.c_3";
		break;
	  case "net":
		selectorName = "div.bug img.c_1, div.bug img.c_2, div.bug img.c_3,div.bug img.b_1, div.bug img.b_2";
		break;
	  case "cleaner":
		selectorName = "div.bug img";
		break;
	}
	$(selectorName).each(function(){
		$parentDiv = $(this).parents("div.bug");
		$parentDiv.addClass("bug_on");
	});
	// 记录当前被点击的按钮名
	activeBtnName = name;
	return true;
}
//动画效果
function imageAnimation(obj){
	//闪动效果1s动画
	autoPlay();
	obj.removeClass("bug_on");
	obj.siblings("div."+activeBtnName).addClass(activeBtnName+'_on');
	obj.siblings("div.light").addClass("light_on");
	getNum(obj);
	//console.log(activeBtnName);
	window.animate = setTimeout(function(){
		// 去看闪动效果动画
		obj.siblings("div."+activeBtnName).removeClass(activeBtnName+'_on');
		obj.siblings("div.light_on").removeClass("light_on");
		var bug = setBug();
        setTimeout(function(){
            obj.parents("li").addClass("disable");
            setTimeout(function(){ 
                obj.parents("li").removeClass("disable");
                obj.find('img').attr({"src":"images/"+bug+".png",'class':bug});
            },1000);
        },0);
//		obj.find('img').attr({"src":"images/"+bug+".png",'class':bug});
		if(!$("#game_timer").hasClass("playing")){
			clearOn();
			return false;
		}
		if(activeBtnName == 'fire'){
			if(bug.substr(0,1) == 'c'){
				obj.addClass("bug_on");
				return false;
			}else{
				check();
			}
		}else if(activeBtnName == 'net'){
			if(bug.substr(0,1) == 'c' || bug.substr(0,1) == 'b'){
				obj.addClass("bug_on");
				return false; 
			}else{
				check();
			}
		}else{
			obj.addClass("bug_on");
			return false; 
		}
	},1000);
}
//点击消除后随机返回
function setBug(){
	var bugData = ['a_1','a_2','b_1','b_2','c_1','c_2','c_3'];
	var num =  Math.floor(Math.random()*10);
	if(num == 0 || num == 1){
		return bugData[4]
	}else if(num == 2 || num == 3){
		return bugData[5]
	}else if(num == 4 || num == 5){
		return bugData[6]
	}else if(num == 6){
		return bugData[3]
	}else if(num == 7){
		return bugData[2]
	}else if(num == 8){
		return bugData[1]
	}else if(num == 9){
		return bugData[0]
	}
}
// 检查是否正在播放
var isPlaying = false;
function autoPlay() {
	var audio = document.createElement("audio");
	audio.src = 'file/sound.mp3';
	if (isPlaying){
		// 如果正在播放, 停止播放并停止读取此音乐文件
		audio.pause();
		audio.src = '';
	} else {
		audio.src = 'file/sound.mp3';
		audio.play();
	}
}
//点击消除
function doCleanUp(clickItem){
	//闪烁可点击
	if($(clickItem).hasClass("bug_on")){
		imageAnimation($(clickItem));
	}
}
//清除所有效果
function clearOn(){
	$(".fire").removeClass(".fire_on");
	$(".light").removeClass(".light_on");
	$(".net").removeClass(".net_on");
	$(".cleaner").removeClass(".cleaner_on");
	$(".bug").removeClass("bug_on");
}

function currentTimeSeconds(){
	return new Date().getTime();
}

// 游戏计时器
function startTime(name){
	$('.game_time').addClass("playing");
	switch(name){
		case "fire":
			rightMSeconds = 6000;
			break;
		case "net":
			rightMSeconds = 4000;
			break;
		case "cleaner":
			rightMSeconds = 2000;
			break;
	}
	endMSeconds = currentTimeSeconds() + rightMSeconds;
	window.timer=setInterval(function(){
		currentMSeconds  = currentTimeSeconds();
		leftMSeconds = endMSeconds - currentMSeconds;
		// console.log("leftMSeconds: "+leftMSeconds);
		if(leftMSeconds > 0){
			$(".time_bar").css({width:leftMSeconds/rightMSeconds*100+"%"}); //进度条
      $(".time_bar span").show();
		}else{
			clearInterval(window.timer);
			$('.game_time').removeClass("playing");
            $(".time_bar span").hide();
            $(".game_btn>div").removeClass("opacity");
			clearOn();
			checkGame();//判断游戏结束
			sendData();//倒计时结束--数据提交
		}
	}, 10);
   
}
//获取分数
window.data = [];
window.num = 0;
window.clear = 0;
function getNum(obj){
	var type = obj.find('img').attr('class').substr(0,1);
	window.data.push(obj.find('img').attr('class'));
	if(type == 'c'){
		window.num += 300;
	}else if(type == 'b'){
		window.num += 600;
	}else if(type == 'a'){
		window.num += 1500;
	}
	window.clear++;
	if(window.clear == 5){  //消除5个--数据提交
		window.clear = 0;
		sendData();
	}
	$("#game_score").text(window.num);
}
//判断是够消除完
function check(){
	var size = $(".bug_on").size();
	if(size == 0){ //消除完毕
		clearInterval(window.timer);
        $(".game_btn>div").removeClass("opacity");
		$('.game_time').removeClass("playing");
		$(".time_bar").css({width:0});
		checkGame();//判断游戏是否完成
		//所有可消灭的怪物已消灭--数据提交
		sendData();
	}
}
//向后台传输数据
function sendData(){
	console.log(window.data)
	$.ajax({
		type:"GET",
		url: "",
		dataType: 'JSON',
		data: window.data,
		success: function(json_data){
			
		},error: function(){
		 
		}
    });
}
//判断游戏是否结束
function checkGame(){
	var size = $(".game_btn .enabled").size();
	if(size == 3){ 		//三个道具已用完
		$(".over_time").text(window.num);
		$("#game_over").show();
	}
}